<template>
  <div>
    <!-- 面包屑   -->
    <div style="margin-bottom: 30px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>物资采购</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <h2>物资采购 <i class="el-icon-s-goods"></i></h2>
    <el-divider></el-divider>

    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd" class="mr-5">提交新增采购单 <i class="el-icon-circle-plus-outline"></i>
      </el-button>
    </div>
    <!-- 表的主体   -->
    <el-table :data="tableData" border stripe
              @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="40">
      </el-table-column>
      <el-table-column prop="id" label="id">
      </el-table-column>
      <el-table-column prop="material.name" label="耗材编号">
      </el-table-column>
      <el-table-column prop="purchaseNum" label="采购数量">
      </el-table-column>
      <el-table-column prop="purchaseDate" label="采购时间">
      </el-table-column>
      <el-table-column prop="money" label="总金额">
      </el-table-column>
      <el-table-column prop="handlePeople.trueName" label="采购人">
      </el-table-column>
      <el-table-column prop="remark" label="备注">
      </el-table-column>
      <el-table-column prop="status" label="采购状态">
        <template slot-scope="scope">
          {{scope.row.status | status}}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="" align="center" min-width="100">
        <template slot-scope="scope">
        <el-popconfirm
            confirm-button-text='确定'
            cancel-button-text='取消'
            icon="el-icon-info"
            icon-color="red"
            title="确定完成采购吗？"
            @confirm="handleEdit(scope.row)">
          <el-button type="primary" slot="reference" :disabled="!!scope.row.status">确认完成采购<i class="el-icon-check"></i></el-button>
        </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码导航栏   -->
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 15, 20]"
          :page-size=pageSize
          layout="total, sizes, prev, pager, next, jumper"
          :total=total>
      </el-pagination>
    </div>
    <!--        物资采购信息的弹窗-->
    <el-dialog title="物资采购单" :visible.sync="dialogFormVisible" width="50%" :close-on-click-modal="false">
      <el-form :model="materialPurchase" ref="materialPurchase" label-width="100px" class="demo-dynamic">
        <el-form-item label="物资" prop="" label-width="100px">
          <el-select v-model="material" placeholder="请选物资" value-key="id" @change="OMchange">
            <el-option
                v-for="item in materials"
                :key="item.id"
                :label="item.name"
                :value="item">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="采购数量" label-width="100px" prop="purchaseNum" v-show="material.id">
          <el-input-number v-model="materialPurchase.purchaseNum" @change="handleChange" :min="1" label="描述文字"></el-input-number>
        </el-form-item>
        <el-form-item label="采购时间" label-width="100px" prop="purchaseDate">
          <el-date-picker
              v-model="materialPurchase.purchaseDate"
              type="date"
              placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="总金额" label-width="100px" prop="money">
          <el-input v-model="materialPurchase.money" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="采购人" prop="vehicleId" label-width="100px">
          <el-select v-model="materialPurchase.staffId" placeholder="采购人">
            <el-option
                v-for="item in staff"
                :key="item.staffId"
                :label="item.trueName"
                :value="item.staffId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" label-width="100px" prop="remark">
          <el-input v-model="materialPurchase.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible=false">取 消</el-button>
        <el-button type="primary" @click="save('materialPurchase')">{{ btnText }}</el-button>
      </div>
    </el-dialog>
  </div>


</template>


<script>
import {CodeToText, regionData, TextToCode} from "element-china-area-data";
import request from "@/utils/request";
import {country} from "@/js/country";

export default {
  name: "MaterialPurchase",
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 5,
      total: 0,
      dialogFormVisible: false,
      btnText: "",
      materialPurchase: {
      },
      multipleSelection: [],
      areaCodes: [],
      isEditingArea: false,
      options: regionData,
      country: country,
      materials: [],
      staff:[],
      material:{},
      rules: {
        // name: [
        //   {required: true, message: '请输入姓名', trigger: 'blur'},
        // ],
        // gender: [
        //   {required: true, message: '请选择性别', trigger: 'blur'},
        // ],
        // phone: [
        //   {required: true, message: '请输入电话号码', trigger: 'blur'},
        //   {min: 11, max: 11, message: '请输入长度为11个数字的电话号码', trigger: 'blur'}
        // ],
        // username: [
        //   {required: true, message: '请输入用户名', trigger: 'blur'},
        //   {min: 3, message: '长度在 3 个字符以上', trigger: 'blur'}
        // ],
        // password: [
        //   {required: true, message: '请输入密码', trigger: 'blur'},
        //   {min: 6, message: '长度在 6 个字符以上', trigger: 'blur'}
        // ]
      },
    }
  },
  filters:{
    status(s){
      if(s){
        return '完成采购'
      }else {
        return '未完成'
      }
    }
  },
  created() {
    this.load();
    request.get("/materialInfo/get",
    ).then(res => {
      this.materials = res.data
    })
    request.get("/staff/get").then(res => {
      this.staff = res.data
    })
  },
  methods: {
    // 分页查询接口
    load() {
      request.get("/materialPurchase",
          {params: {pageNum: this.currentPage, pageSize: this.pageSize}}
      ).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
        this.currentPage = res.data.current
        this.pageSize = res.data.size
      })
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum
      this.load()
    },
    handleAdd() {
      this.areaCodes = []
      this.btnText = "添加"
      this.isEditingArea = true
      this.materialPurchase = {}
      this.dialogFormVisible = true
    },
    handleEdit(row) {
      this.materialPurchase = Object.assign({}, row)
      this.materialPurchase.status = 1
      // console.log(this.materialPurchase)
      request.put("/materialPurchase", this.materialPurchase).then(res => {
        if (res) {
          this.$message.success('采购状态已更改')
          this.load()
        } else {
          this.$message.error("更改采购状态失败")
        }
      })
    },
    showAreaEdit() {
      this.isEditingArea = !this.isEditingArea
    },
    // 增加修改数据接口
    save(materialPurchase) {
      this.$refs[materialPurchase].validate((valid) => {
        if (valid) {
          this.dialogFormVisible = false
          if (this.btnText === '添加') {
            this.materialPurchase.payState = 0
            this.materialPurchase.status = 0
            request.post("/materialPurchase", this.materialPurchase).then(res => {
              if (res) {
                this.$message.success(this.btnText + "成功")
                this.load()
              } else {
                this.$message.error(this.btnText + "失败")
              }
            })
          } else {
            request.put("/materialPurchase/update", this.materialPurchase).then(res => {
              if (res) {
                this.$message.success(this.btnText + "成功")
                this.load()
              } else {
                this.$message.error(this.btnText + "失败")
              }
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 单个删除
    del(id) {
      request.delete("/materialPurchase/del/" + id).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
      console.log(id)
    },
    handleSelectionChange(val) {
      // console.log(val)
      this.multipleSelection = val
    },
    // 批量删除接口
    delBatch() {
      //map函数：数组的每个元素都执行一个给定的函数，返回值仍为一个数组
      let ids = this.multipleSelection.map(v => v.id)
      console.log(ids)
      if (ids.length !== 0) {
        request.delete("/materialPurchase/del/" + ids.toString(),).then(res => {
          if (res) {
            this.$message.success("批量删除成功")
            this.load()
          } else {
            this.$message.error("批量删除失败")
          }
        })
      } else {
        this.$message.warning("请选择要删除的数据")
      }
    },
    reset() {
      this.material={}
      this.load()
    },
    OMchange(item){
      this.materialPurchase.materialId = item.id;
    },
    handleChange(num){
      this.materialPurchase.money = (this.material.materialprice.inPrice * num).toFixed(2)
    }
  }
}
</script>

<style scoped>

</style>